package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomerException;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.pojo.Category;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;

    @Override
    public void deleteById(Long id) {
        //1. 判断是否有菜品关联分类 select count(*) from dish where category_id=?
        Integer count1 = dishService.lambdaQuery()
                .eq(Dish::getCategoryId, id) //条件部分
                .count();//查询数量
        if (count1 > 0) {
            //让前端知道出问题。抛异常
            throw new CustomerException("该分类有关联菜品，不能删除！");
        }

        //2. 判断是否有套餐关联分类 select * from setmeal where category_id=?
        Integer count2 = setmealService.lambdaQuery()
                .eq(Setmeal::getCategoryId, id)
                .count();
        if (count2 > 0) {
            //让前端知道出问题。抛异常
            throw new CustomerException("该分类有关联套餐，不能删除！");
        }


        //3. 删除分类 delete from category where id=?
        this.removeById(id);

    }
}
